import { Scene } from "three";
function getTags(scene:Scene){
  const tags=new Map();
  scene.traverse(object=>{
    if(object.userData&&object.userData.tags&&Array.isArray(object.userData.tags)){
      object.userData.tags.forEach(tag=>{
        // tags.set(tag)
      })
    }
  })
}

export class TezeroMQTT{
  scene:Scene;
  client?:any;
  connected:boolean=false;
  topic:string;
  constructor(scene:Scene,topic?:string){
    this.scene=scene;
    this.topic=topic||'tezero';
  }
  connect(brokerUrl:string,opts?:any){
    const mqtt=(window as any).mqtt;
    if(!mqtt){
      console.warn('<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>');
      return;
    }
    this.client=mqtt.connect(brokerUrl,opts);
    console.log('client',this.client);
    this.client.on("connect", () => {
      console.log("connected");
      this.connected = true
      this.client.subscribe(this.topic, (err:any) => {
        if (!err) {

          this.client.publish("tezero", "Hello mqtt");
        }
      });
    });
    
    this.client.on("message", (topic:any, message:any) => {
      console.log('message', topic, message.toString());
      // message is Buffer
    });
    
    this.client.on("close", () => {
      console.log("close");
      this.connected = false
    });
  }

  
  
}